package com.tyut.homework.service.impl;

import com.tyut.homework.pojo.Apartment;
import com.tyut.homework.repository.ApartmentRepostory;
import com.tyut.homework.service.ApartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApartmentServiceImpl implements ApartmentService {

    @Autowired
    private ApartmentRepostory apartmentRepostory;

    @Override
    public Apartment save(Apartment apartment) {
        return apartmentRepostory.save(apartment);
    }

    @Override
    public int delete(Integer id) {
        Apartment apartment = new Apartment();
        apartment.setId(id);
        apartmentRepostory.delete(apartment);
        return 1;
    }

    @Override
    public int update(Apartment apartment) {
        apartmentRepostory.saveAndFlush(apartment);
        return 1;
    }

    @Override
    public Apartment find(Integer id) {
        return apartmentRepostory.findById(id).get();
    }

    @Override
    public List<Apartment> findAll() {
        return null;
    }

    @Override
    public Page<Apartment> getPage(Integer pageNum, Integer pageLimit) {
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(pageNum, pageLimit, sort);
        return apartmentRepostory.findAll(pageable);
    }
}
